<?php

App::uses('AppController', 'Controller');

/**
 * Quizzes Controller
 *
 * @property Quiz $Quiz
 */
class QuizzesController extends AppController {

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index($id = null) {
        if (!$id)
            $this->redirect(array("controller" => "subjects", 'action' => 'index'));
        $this->Quiz->recursive = -1;
        $conditios = array("Quiz.subject_id" => $id);
        $this->set('quizzes', $this->paginate($conditios));
        $type = 1;
        $this->set("id", $id);
        $this->set("type", $type);
    }

    public function admin_qlesson($id = null) {
        if (!$id)
            $this->redirect(array("controller" => "lessons", 'action' => 'index'));
        $this->Quiz->recursive = -1;
        $conditios = array("Quiz.lesson_id" => $id);
        $this->set('quizzes', $this->paginate($conditios));
        $this->set("id", $id);
        $type = 3;
        $this->set("type", $type);
        $this->render("admin_index");
    }

    public function admin_qlevel($id = null) {
        if (!$id)
            $this->redirect(array("controller" => "levels", 'action' => 'index'));
        $this->Quiz->recursive = -1;
        $conditios = array("Quiz.study_level_id" => $id);
        $this->set('quizzes', $this->paginate($conditios));
        $type = 2;
        $this->set("id", $id);
        $this->set("type", $type);
        $this->render("admin_index");
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Quiz->id = $id;
        if (!$this->Quiz->exists()) {
            throw new NotFoundException(__('Invalid data'));
        }
        $this->set('quiz', $this->Quiz->read(null, $id));
        $this->layout = "quize";
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add($id = null, $type = null) {
        if (!$id || !in_array($type, array(1, 2, 3)))
            $this->redirect(array("controller" => "levels", 'action' => 'index'));

        $this->set("id", $id);
        $this->set("type", $type);
        $this->request->data["Quiz"]["quiz_type"] = $type;
        switch ($type) {
            case 1:
                $this->request->data["Quiz"]["subject_id"] = $id;
                break;
            case 2:
                $this->request->data["Quiz"]["study_level_id"] = $id;
                break;
            case 3:
                $this->request->data["Quiz"]["lesson_id"] = $id;
                break;

            default:
                break;
        }
        if ($this->request->is('post')) {
            $this->Quiz->create();
            if ($this->Quiz->save($this->request->data)) {
                $id = $this->Quiz->id;
                $this->savelog($id);
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        }
        $studyLevels = $this->Quiz->StudyLevel->find('list');
        $lessons = $this->Quiz->Lesson->find('list');
        $subjects = $this->Quiz->Subject->find('list');
        $this->set(compact('studyLevels', 'lessons', 'subjects'));
    }

    public function admin_creator() {
        $this->layout = "quize";
        if ($this->request->is('post')) {
            debug($this->request->data);
            die;

            if (isset($this->request->data["Quiz"]["user_id"])) {
                //$useId =AuthComponent::user('id');		
                $useId = "1";
                $this->request->data["Quiz"]["user_id"] = $useId;
            }

            $this->Quiz->create();
            if ($this->Product->saveAssociated($this->request->data)) {
                $id = $this->Quiz->id;
                $this->savelog($id);
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        }
        $studyLevels = $this->Quiz->StudyLevel->find('list');
        $lessons = $this->Quiz->Lesson->find('list');
        $subjects = $this->Quiz->Subject->find('list');
        $this->set(compact('studyLevels', 'lessons', 'subjects'));
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Quiz->id = $id;
        if (!$this->Quiz->exists()) {
            throw new NotFoundException(__('Invalid quiz'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {

            if (isset($this->request->data["Quiz"]["user_id"])) {
                //$useId =AuthComponent::user('id');		
                $useId = "1";
                $this->request->data["Quiz"]["user_id"] = $useId;
            }

            if ($this->Quiz->save($this->request->data)) {
                $this->savelog($id);
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        } else {
            $this->request->data = $this->Quiz->read(null, $id);
        }
        $studyLevels = $this->Quiz->StudyLevel->find('list');
        $lessons = $this->Quiz->Lesson->find('list');
        $subjects = $this->Quiz->Subject->find('list');
        $this->set(compact('studyLevels', 'lessons', 'subjects'));
        $this->render("admin_add");
    }

    /**
     * admin_delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Quiz->id = $id;
        if (!$this->Quiz->exists()) {
            throw new NotFoundException(__('Invalid quiz'));
        }
        if ($this->Quiz->delete()) {
            $this->savelog($id);

            $this->Session->setFlash(__('Data has been deleted'), 'default', array("class" => "mws-form-message success"));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
        $this->redirect(array('action' => 'index'));
    }

}
